<script>
export default{
    data() {
        return {
            num: 0,
            obj: [
                {
                    name: 'zs',
                    id: 1,
                    num: 1,
                    price: 10
                },
                {
                    name: 'zs1',
                    id: 2,
                    num: 2,
                    price: 20
                },
                {
                    name: 'zs2',
                    id: 3,
                    num: 3,
                    price: 30
                },
                {
                    name: 'zs3',
                    id: 4,
                    num: 4,
                    price: 40
                }
            ]
        }
    },
    methods:{
        add(index){
            this.obj[index].num++
        },
        sub(index){
            if(this.obj[index].num > 0) {
                this.obj[index].num--
            }
        }
        number(){
            let sum =0
            obj.forEach(item=>{
                sum += item.price * item.num 
                ruter sum
            })
        }
    }
}
</script>

<template>
<!-- <div @click="add">初始值{{ num }}</div> -->
<table border>
      <thead>
        <tr>
          <th>id</th>
          <th>名字</th>
          <th>单价</th>
          <th>数量</th>
          <th>合计</th>
          
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in obj" :key="index">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.price }}</td>
          <td><span @click="sub(index)">-</span> {{ item.num }} <span @click="add(index)">+</span></td>
          <td>{{ item.price * item.num }}</td>
        </tr>
      </tbody>
      <div>总和{{ sum }}</div>
    </table>

<table></table>
</template>

<style scoped>

</style>
